Ext.namespace("Task.Open");

Task.Open.panel = function(taskOrigin,outTime) {
	Task.FeedBack.win.instance = new Task.FeedBack.win();
	this.txtTaskTitle = new Ext.form.TextField({
		width: 150
	});
	this.dtStart = new Ext.form.DateField({
		width: 85,
		format: 'Y-m-d'
	});
	this.dtEnd = new Ext.form.DateField({
		width: 85,
		format: 'Y-m-d'
	});
	this.dtFulfill = new Ext.form.DateField({
		width: 85,
		format: 'Y-m-d'
	});
    this.storeOrigin=new Ext.data.ArrayStore({
	        	fields: ['value', 'name'],
	        	data: OMS.TASKORIGINVALUE
	    });	
	this.cbOrigin = new Ext.form.ComboBox({
		width: 100,
		name:'taskOrigin',
		store:this.storeOrigin ,
		value:taskOrigin,
		valueField: 'value',
        displayField: 'name',
        editable: false,
        mode: 'local',
        triggerAction: 'all',
        selectOnFocus: true
	});
	this.storeOrigin.insert(0,new this.storeOrigin.recordType({value:'',name:'全部'}, 101));
	
	this.cbOuttime = new Ext.form.Checkbox({
		name:'outTime'
	});
	
	if(outTime)
		this.cbOuttime.setValue(true);	
	//this.cbOuttime.fireEvent("check", this.cbOuttime, true);
    this.btnSearch = new Ext.Toolbar.Button({
        iconCls: 'icon-search',
        text: '查找',
        tooltip: {text: '查找任务'},
        handler: this.queryTask,
        scope: this
    });
    
    this.btnSend = new Ext.Toolbar.Button({
        iconCls: 'icon-feedback',
        text: '反馈',
        tooltip: {text: '任务反馈'},
        handler: this.feedBackTask,
        scope: this
    });
    this.btnChange = new Ext.Toolbar.Button({
        iconCls: 'icon-forward',
        text: '转发',
        tooltip: {text: '任务转发'},
        handler: this.changeTask,
        scope: this
    });
	this.store = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({
			url: '../task/searchTaskExe.form?timesnap=' + Date.parse(new Date())
		}),
		reader: new Ext.data.JsonReader({
			root: 'result',
			totalProperty: 'total',
			fields: ['TASK_ID', 'TASK_TITLE', 'TASK_DATE','USER_NAME','TASK_COMPLETE_DATE', 'TASK_DISPATCHER',
			    'TASK_CONTENT','TASK_STATUS','TASK_ORIGIN','TASK_NOTE','TASK_EXECUTOR','TASK_EXE_ID','TASK_EXE_STATUS','TASK_EXE_PERCENT',
			    'TASK_EXE_USER_ID','TASK_EXE_DISPATCHER_ID','DIS_NAME','TASK_EXE_STATE','HAS_ACCESSORY']
		}),listeners:{
			 beforeload :{    
	    	  	fn : function(thiz,options){	    	  		
       	          Ext.apply(thiz.baseParams,
       	          { 
       	           	 status:[1],
       	             taskTitle:this.txtTaskTitle.getValue(),
			  		 dtStart:this.dtStart.getValue()?this.dtStart.getValue().format('Y-m-d'):'',
			   		 dtEnd:this.dtEnd.getValue()?this.dtEnd.getValue().format('Y-m-d'):'',
			   		 dtFulfill:this.dtFulfill.getValue()?this.dtFulfill.getValue().format('Y-m-d'):'',
			   		 outTime:this.cbOuttime.getValue()?'1':''
       	          });    
              },
              scope:this 
	    	}
		}
	});
	this.renderDays = function(value){
		if(!value)
			return;
		var date = Date.parseDate(value.substring(0,10),"Y-m-d");
		var days = parseInt((date.getTime()-new Date().getTime())/(1000*60*60*24));
		if(days<0)
			return "<font color='red' size=1>逾期"+(-days)+"天</font>";
		else
			return "<font size=1>还剩"+days+"天</font>";
	};
	this.renderTaskStatus = function(value){
		return OMS.TASKSEXESTATUS[value];
	}
	this.renderOrigin = function(value){
		return OMS.TASKORIGIN[value];
	}
	this.colModel = new Ext.grid.ColumnModel({
	    defaults: {
	        width: 100,
	        sortable: true,
	        align: 'center'
	    },
	    columns: [
			new Ext.grid.CheckboxSelectionModel(),
			{header: 'TASK_ID', width: 50, hidden: true, dataIndex: 'TASK_ID'},
			{header: '附件', width: 30, dataIndex: 'HAS_ACCESSORY',renderer:this.renderAccessory},
			{header: '任务名称',align:'left', width: 200, dataIndex: 'TASK_TITLE'},
			{header: '派发时间', dataIndex: 'TASK_DATE'},
			{header: '完成时限', dataIndex: 'TASK_COMPLETE_DATE'},
			{header: '派发人', dataIndex: 'DIS_NAME'},
			{header: '剩余天数', dataIndex: 'TASK_COMPLETE_DATE',renderer:this.renderDays},
			{header: '任务状态', dataIndex: 'TASK_EXE_STATUS',renderer:this.renderTaskStatus},
			{header: '任务来源', dataIndex: 'TASK_ORIGIN',renderer:this.renderOrigin}
	    ]
	});
	
	this.gridPanel = new Ext.grid.GridPanel({
		region: 'center',
		cm:this.colModel,
		store:this.store,
		loadMask: true,
		stripeRows: true, 
		tbar: [this.btnSend,'-',this.btnChange,'->','任务名称:', this.txtTaskTitle, 
		          ' 派发时间:', this.dtStart, '~', this.dtEnd, '任务来源',this.cbOrigin,'逾期任务:',this.cbOuttime,this.btnSearch],
		sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
        loadMask: {msg: '加载中，请稍候...'},
        bbar:new Ext.PagingToolbar({
			 pageSize: 15,
	         store: this.store,
	         displayInfo: true,
	         displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
	         emptyMsg: "没有记录..."
		}),
        viewConfig: { forceFit: true },
        listeners:{
      		  rowdblclick : function( grid, rowIndex,e){ 
        			var data = grid.store.getAt(rowIndex).data;
        			data.TASK_EXE_STATE ="";
        			Task.FeedBack.win.instance.initFeedBack(data);
					Task.FeedBack.win.instance.show();
			}
       }
	})
	Task.Open.panel.superclass.constructor.call(this, {
		title:'待办任务',
		layout:'border',
		items:[this.gridPanel],
		listeners:{
			//render:this.queryTask
			activate:this.queryTask
		}
	});
};
Ext.extend(Task.Open.panel, Ext.Panel, {
	queryTask :function(){
		this.store.load({
			params:{
				start : 0,
				limit : 15,
				status:[1],
				taskTitle:this.txtTaskTitle.getValue(),
			    dtStart:this.dtStart.getValue()?this.dtStart.getValue().format('Y-m-d'):'',
			    dtEnd:this.dtEnd.getValue()?this.dtEnd.getValue().format('Y-m-d'):'',
			    dtFulfill:this.dtFulfill.getValue()?this.dtFulfill.getValue().format('Y-m-d'):'',
			    taskOrigin:this.cbOrigin.getValue(),
			    outTime:this.cbOuttime.getValue()?'1':''
				}
		});
	},
	feedBackTask:function(){
		var sm =this.gridPanel.getSelectionModel();
		var record = sm.getSelections();
		if(record.length==0){
			Utils.warn('请选择一条任务！','警告');
			return ;
		}
		else if(record.length>1){
			Utils.warn('只能选择一条任务！',"警告");
			return;
		}
		Task.FeedBack.win.instance.initFeedBack(record[0].data);
		Task.FeedBack.win.instance.show();
	},
	changeTask:function(){
		var sm =this.gridPanel.getSelectionModel();
		var record = sm.getSelections();
		if(record.length==0){
			Utils.warn('请选择一条任务！','警告');
			return ;
		}
		else if(record.length>1){
			Utils.warn('只能选择一条任务！',"警告");
			return;
		}
		Task.FeedBack.win.instance.initChange(record[0].data);
		Task.FeedBack.win.instance.show();
	},
    renderAccessory : function(value) {
		if (value != null && value == '0') {
			return "@";
		} else {
			return "";
		}
	}
	
});


